﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Web;
using QDT.Common;

namespace QDT.Payment
{
    public abstract class AbstractPayment : IPayment
    {
        /// <summary>
        /// 识别Key,一般为类名
        /// </summary>
        public abstract string Key { get; }

        /// <summary>
        /// 名字
        /// </summary>
        public abstract string Name {get; }

        /// <summary>
        /// 是否是在线支付
        /// </summary>
        public bool OnlinePaly { get;protected set; }

        /// <summary>
        /// 手续费计算方式（0：定额  1：比率）
        /// </summary>
        public int FeeType { get; set; }

        /// <summary>
        /// 手续费值
        /// </summary>
        public decimal FeeAmount { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 发送支付请求到支付网关
        /// </summary>
        /// <param name="order">支付订单信息</param>
        /// <returns></returns>
        public abstract void InchargeRequest(PaymentOrder order);

        public abstract bool InchargeQueryRequest(PaymentOrder order);

        /// <summary>
        /// 发送提现请求到支付网关
        /// </summary>
        /// <param name="orders">提现订单信息</param>
        /// <returns></returns>
        public abstract void CarryRequest(IEnumerable<CarryOrder> orders);

        public abstract bool CarryRequest(CarryOrder order);

        /// <summary>
        /// 发送提现查询到支付网关
        /// </summary>
        /// <param name="orderSn">提现订单流水号</param>
        /// <returns></returns>
        public abstract int CarryQueryRequest(string orderSn);

        /// <summary>
        /// 验证交易是否被篡改
        /// </summary>
        /// <param name="form">第三方支付传递的参数</param>
        /// <param name="order"></param>
        /// <returns></returns>
        public abstract bool VerifyNotify(NameValueCollection form, out object order);

        /// <summary>
        /// 验证交易是否被篡改，并返回第三方字符的文本,主要用于自动Notice
        /// </summary>
        /// <param name="form"></param>
        /// <param name="returnContent">返回的内容</param>
        /// <param name="order"></param>
        /// <returns></returns>
        public abstract bool VerifyNotify(NameValueCollection form, out string returnContent, out object order);

        private const string FormFormat = "<form id=\"payform\" name=\"payform\" action=\"{0}\" method=\"POST\">{1}</form>";
        private const string InputFormat = "<input type=\"hidden\" id=\"{0}\" name=\"{0}\" value=\"{1}\" />";
        private const string SubmitInput = "<input type=\"submit\" value=\"在线支付\" style=\"display:none;\" />";
        private const string SubmitScript = "<script>document.forms['payform'].submit();</script>";

        protected virtual string CreateField(string name, string strValue)
        {
            return string.Format(CultureInfo.InvariantCulture, InputFormat, new object[] { name, strValue });
        }

        protected virtual string CreateForm(string content, string action)
        {
            content = content + SubmitInput;
            return string.Format(CultureInfo.InvariantCulture, FormFormat, new object[] { action, content });
        }

        protected virtual void SubmitPaymentForm(string formContent)
        {
            string s = formContent + SubmitScript;
            HttpContext.Current.Response.Write(s);
            HttpContext.Current.Response.End();
        }

        protected virtual string GenerateOrderNumber()
        {
            var date = DateTime.Now;
            var rd = new Random().Next(1, 999);
            return  "C{0}{1}".FormatWith(date.ToString("yyMMddHHmmssffff"), rd.ToString("000"));
        }
        
    }
}
